<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入员工姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.phone" placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.dname" placeholder="请输入部门"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="filters.state" placeholder="请选择" clearable>
            <el-option label="正常" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getQueryEmps">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item label="" prop="resource">
          <el-col :span="12">
            <el-upload
                class="upload-demo"
                action="http://localhost:8080/emp/importExcel"
                list-type="picture">
              <el-button  type="primary">点击导入</el-button>
            </el-upload>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleExport">导出</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="emps" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工姓名" width="180" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="180" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="180" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="180" sortable>
        <template slot-scope="scope">
          <div slot="reference" v-if="scope.row.state == 1">
            <el-tag size="medium" type = "success">正常</el-tag>
          </div>
          <div slot="reference" v-if="scope.row.state == 0">
            <el-tag size="medium" type = "danger">禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="department.name" label="部门" width="180" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">-->
      <!--			</el-pagination>-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right">
      </el-pagination>
    </el-col>

    <!--保存界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">正常</el-radio>
            <el-radio class="radio" :label="0">离职</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门" prop="department_id">
          <div class="block">
            <span class="demonstration"></span>
            <el-cascader
                v-model="saveForm.departmentId"
                :options="deptTree"
                :props="{ checkStrictly: true,
                          label:'name',
                          value:'id'
                        }"
                clearable></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../../common/js/util'
//import NProgress from 'nprogress'
import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../../api/api';

export default {
  data() {
    return {
      filters: {
        keyword: '',
        dname:'',
        state:'',
        phone:'',
      },
      emps:[],//员工
      deptTree:[],//获取部门树
      currentPage: 1,
      pageSize:5,
      pageSizes:[5, 10, 15, 20],
      total:0,
      listLoading: false,
      title:'',
      sels: [],//列表选中列

      saveFormVisible: false,//新增界面是否显示
      saveLoading: false,
      saveFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //保存界面数据
      saveForm: {
        id: null,
        username:'',
        email:"",
        phone: '',
        age:'',
        state: 1,
        departmentId: null,
      }

    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEmps();

    },
    handleSizeChange(val){
      this.pageSize = val
      this.getEmps()
    },
    //高级查询刷新列表
    getQueryEmps(){
      this.currentPage = 1
      this.getEmps()
    },
    //获取员工列表
    getEmps() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        dname:this.filters.dname,
        phone:this.filters.phone,
        keyword:this.filters.keyword,
        state:this.filters.state
      };
      this.listLoading = true;
      this.$http.post("/emp/list",para).then(result=>{
        console.log(result)
        this.emps = result.data.data
        this.total = result.data.total
        this.listLoading = false;
      })
    },
    //获取部门树
    getdeptTree(){
      this.$http.get("/dept/deptTree").then(result=>{
        console.log(result)
        this.deptTree =result.data
      })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        console.log(row.id)
        this.$http.delete("/emp/"+row.id).then(result=>{
          if (result.data.success){
            this.$message({
              message: result.data.msg,
              type: 'success'
            });
            this.currentPage = 1
            this.getEmps()
          }else {
            this.$message({
              message: result.data.msg,
              type: 'error'
            });
          }
          this.listLoading = false;
        })
      }).catch(() => {
        this.listLoading = false;
      });
    },
    //显示编辑界面
    handleEdit(index, row){
      if(this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();//重置表单验证
      }
      this.title = "编辑"
      this.saveFormVisible = true;
      this.saveForm = Object.assign({},row)
    },
    //显示新增界面
    handleAdd: function () {
      if(this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();//重置表单验证
      }
      this.title = "新增"
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        username:'',
        email:"",
        phone: '',
        age:'',
        state: 1,
        departmentId: null,
      };
    },
    //保存
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            if (para.departmentId){
              para.departmentId = para.departmentId[para.departmentId.length - 1];
            }
            this.$http.put("/emp",para).then(result=>{
              if (result.data.success){
                this.saveLoading = false;
                this.$message({
                  message: result.data.msg,
                  type: 'success'
                });
                //刷新列表
                this.getEmps()
                //关闭模态框
                this.saveFormVisible = false;
              }else {
                this.$message({
                  message: result.data.msg,
                  type: 'error'
                });
                this.saveLoading = false;
              }
            })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true; //开启忙等
        //NProgress.start();
        let para = { ids: ids };
        console.log(ids)
        this.$http.patch("/emp/del",ids).then(result=>{
          this.listLoading = false; //关闭忙等
          //判断是否成功
          if (result.data.success){
            this.$message({
              message: result.data.msg,
              type: 'success'
            });
            //刷新列表
            this.currentPage = 1;
            this.getEmps();
          }else {
            this.$message({
              message: result.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {

      });
    },
    //导出
    handleExport(){
      location.href="http://localhost:8080/emp/export";
    }
  },
  mounted() {
    this.getEmps();
    this.getdeptTree()
  }
}

</script>

<style scoped>

</style>